package jp.hamachi.android.demo.lock;

import java.util.List;

import android.app.KeyguardManager;
import android.appwidget.AppWidgetHost;
import android.appwidget.AppWidgetHostView;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProviderInfo;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;

public class LockUtil implements OnClickListener {
    private static final int APPWIDGET_HOST_ID = 0x100;
    // めんどいからシングルトン
    private static LockUtil instance = new LockUtil();

    private LockUtil() {
    }

    public static LockUtil getInstance() {
        return instance;
    }

    // ロック画面用
    private View mLockView = null;
    private WindowManager mWindowManager = null;

    public void lock(Context context) {

        // 設定値を取得
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        boolean isEnabled = sp.getBoolean("is_enabled", false);

        if (isEnabled) {
            // ロック有効の場合

            // ロック画面を作って
            mLockView = LayoutInflater.from(context).inflate(R.layout.lock, null);
            // ボタン押下時のリスナーを設定
            mLockView.findViewById(R.id.button_unlock).setOnClickListener(this);

            // android.view.WindowManager.LayoutParams
            // ロック画面表示用のパラメータ
            LayoutParams params = new LayoutParams();

            // 全画面
            //            params.width = LayoutParams.FILL_PARENT;
            //            params.height = LayoutParams.FILL_PARENT;
            // SystemAlert
            params.type = LayoutParams.TYPE_SYSTEM_ALERT;

            // 透過
            //            params.format = PixelFormat.TRANSLUCENT;
            // 裏はぼやける
            params.flags = LayoutParams.FLAG_BLUR_BEHIND;

            //********************
            params.systemUiVisibility = View.STATUS_BAR_HIDDEN;
            params.type = LayoutParams.TYPE_SYSTEM_ALERT;
            params.flags = LayoutParams.FLAG_SHOW_WHEN_LOCKED;
            //******************

            // WindowManagerに追加

            if (mWindowManager == null) {
                mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            }

            AppWidgetHost mAppWidgetHost = new AppWidgetHost(context, APPWIDGET_HOST_ID);
            mAppWidgetHost.startListening();

            int appWidgetId = mAppWidgetHost.allocateAppWidgetId();

            Log.d("TAG", "appWidgetId--->" + appWidgetId);

            AppWidgetManager mAppWidgetManager = AppWidgetManager.getInstance(context);
            List<AppWidgetProviderInfo> appWidgetProviderInfo = mAppWidgetManager.getInstalledProviders();

            AppWidgetProviderInfo appWidgetInfo = appWidgetProviderInfo.get(3);
            AppWidgetHostView appWidgetHostView = mAppWidgetHost.createView(context, appWidgetId, appWidgetInfo);
            ((ViewGroup) mLockView).addView(appWidgetHostView);

            mWindowManager.addView(mLockView, params);

        } else {
            // ロックが無効
        }
    }
    public void unlock(Context context) {
        if (mWindowManager == null) {
            mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        }
        // WindowManagerから削除
        mWindowManager.removeView(mLockView);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.button_unlock:
            // 解除ボタン押下時、解除処理を呼ぶ
            unlock(v.getContext());
        }
    }

    // キーガード用
    private KeyguardManager mKeyguard = null;
    private KeyguardManager.KeyguardLock mLock = null;

    public void disableKeyguard(Context _c) {

        // 初期化して
        if (mKeyguard == null) {
            mKeyguard = (KeyguardManager) _c.getSystemService(Context.KEYGUARD_SERVICE);
            mLock = mKeyguard.newKeyguardLock("LockUtil");
        }

        // キーガードを無効化
        mLock.disableKeyguard();

    }

    public void enableKeyguard() {
        // キーガードを有効化
        if (mLock != null) {
            mLock.reenableKeyguard();
        } else {
            // nullの場合は無効化されて無い
        }

    }

}
